package com.exonline.backstage.controller.serverinfo;

import com.exonline.backstage.entity.OnlineInfo;
import com.exonline.commons.dao.DaoImpl;
import com.exonline.commons.dao.IDao;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 *在线控制台数据分析接口
 * @author YUKI
 * @date 2024/12/2
 */

public class OnlineInfoService   {
  ObjectMapper mapper = new ObjectMapper();
  IDao dao = new DaoImpl();

  public OnlineInfo getOnlineInfo(HttpServletRequest req) {
    OnlineInfo onlineInfo = new OnlineInfo();

    int totalUser = 0;
    int onlineUser = 0;
    String totalProfit;
    int totalCourses = 0;

    try{
//     onlineUser = (Integer) req.getServletContext().getAttribute("onlineUsers");
//     totalUser = 0;
//    if (totalUser < onlineUser) {
//      totalUser = onlineUser;
//    }
      //全部课程
     totalCourses = dao.queryCount("SELECT count(id) FROM courses" );
      List<Object[]> list = dao.queryWithPagingAndConditions("SELECT * FROM totalprofit", null, 1, 10, new ArrayListHandler(), null);
      //今日收益
      totalProfit = String.valueOf(list.get(0)[0]);
        //方向占比
      List<Object[]> cdirectionList = dao.queryWithPagingAndConditions("SELECT * FROM cdirectionview", null, 1, 100, new ArrayListHandler(), null);
      Map<String, Object> cdirectionMap = new HashMap<>();
      for (Object[] objects : cdirectionList) {
        cdirectionMap.put(objects[0].toString(), objects[2].toString());
      }


      System.out.println(cdirectionMap);
      onlineInfo.setPercentage(cdirectionMap);
      //科目占比
        List<Object[]> csubjectList = dao.queryWithPagingAndConditions("SELECT * FROM subjectview", null, 1, 100, new ArrayListHandler(), null);

        Map<String,Map<String,Object>> coursedmap = new HashMap<>();
        for (Object[] objects : csubjectList) {
            String cdirection = (String)objects[0];
            String csubject = (String) objects[1];
            String csubjectPrecentage =String.valueOf(objects[3]);
            if (coursedmap.get(cdirection) == null) {
                coursedmap.put(cdirection,new HashMap<>());
                coursedmap.get(cdirection).put(csubject,csubjectPrecentage);
            }else {
                coursedmap.get(cdirection).put(csubject,csubjectPrecentage);
            }
        }
        System.out.println(coursedmap);
        onlineInfo.setSubjectPercentage(coursedmap);
      //每周销售趋势
      List<Map<String,Object>> trendsWeekMap = dao.queryWithPagingAndConditions("SELECT * FROM trendsweekview", null, 1, 10, new MapListHandler(), null);
      System.out.println(trendsWeekMap);
      //年销售趋势
      List<Map<String,Object>> trendsMonthMap = dao.queryWithPagingAndConditions("SELECT * FROM trendsyearview", null, 1, 10, new MapListHandler(), null);
      System.out.println(trendsMonthMap);
      System.out.println(totalUser + "," + onlineUser + "," + totalProfit + "," + totalCourses);
      onlineInfo.setTotalProfit(totalProfit);
      onlineInfo.setTotalCourses(totalCourses);
      onlineInfo.setTrendsweek(trendsWeekMap);
      onlineInfo.setTrendsmonth(trendsMonthMap);
      onlineInfo.setTotalUser(100);
      onlineInfo.setOnlineUser(100);
      return  onlineInfo;
  }catch (SQLException e) {
      throw new RuntimeException("服务器信息查询错误:错误信息" + e.getMessage(), e);
    }
  }
}
